<?php
//require APPPATH . '../../composer/vendor/autoload.php';
//use QL\QueryList;
//use QL\Ext\PhantomJs;

class Service_common extends MY_Service
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('Model_web_crawler_config');
        $this->load->model('Model_common');
    }

    public function index()
    {
        $index = (int)$this->getConfigValByKey("web_index");
        $limit = (int)$this->getConfigValByKey("web_limit");

        $cond = ['type' => 0, 'status' => 0, 'id >' => $index];
        $list = $this->Model_web_crawler_config->getList(1, $cond, $limit);
        if (!is_array($list) || empty($list)) {
            $nextIndex = 0;
            $this->updateConfigValByKey("web_index", $nextIndex);
            return false;
        }

        $lastId = $index;
        foreach ($list as $config) {
            $ql = QueryList::getInstance();
            $obj = $ql->get($config['url']);
            if ($config['js']) {
                $ql->use(PhantomJs::class, '/usr/local/bin/phantomjs');
                $obj = $ql->browser($config['url']);
            }
            $data = $obj->rules(json_decode($config['rules'], true))->range($config['range'])->queryData();
            $this->_addData($data);

            $lastId = $config['id'];
        }

        $nextIndex = $lastId;
        $this->updateConfigValByKey("web_index", $nextIndex);
    }

    public function api()
    {
        $index = (int)$this->getConfigValByKey("web_api_index");
        $limit = (int)$this->getConfigValByKey("web_api_limit");

        $cond = ['type' => 1, 'status' => 0];
        $list = $this->Model_web_crawler_config->getList(1, $cond, $limit);
        if (!is_array($list) || empty($list)) {
            $nextIndex = 0;
            $this->updateConfigValByKey("web_api_index", $nextIndex);
            return false;
        }

        $lastId = $index;
        $this->load->service('Service_api');
        foreach ($list as $config) {
            $serviceApi = $this->Service_api->setInfo($config);
            // 请求解析数据
            $data = $serviceApi->queryData();
            // 存储数据结果
            $serviceApi->addData($data);
            // 全量时，处理成下一页
            $serviceApi->dealNext();
            $lastId = $config['id'];
        }

        $nextIndex = $lastId;
        $this->updateConfigValByKey("web_api_index", $nextIndex);
    }
}